<?php
class UserAction extends BaseAction{
	/**
	 * Show all administrator 
	 */
	public function index(){
		$User = M('User');
		$users = $User->select();
		$this->assign('users',$users);
		$this->display();
	}
	/**
	 * Delete a user
	 */
	public function delete(){
		$User = M('User');
		$condition['uid'] = $_POST['id'];
		$res = $User->where($condition)->delete();
		if($res){
			$this->redirect('index', '',5,'Delete user succeed');
		}else{
			$this->redirect('index', '',5,'Delete user not succeed');
		}
	}
	/**
	 * To register a new administrator
	 */
	public function add(){
		$User = M('User');
		$data['username'] = $_POST['username'];
		$data['password'] = md5($_POST['password']);
		$data['state']=1;
		$isReg=$User->where('username="'.$data['username'].'"')->find();
		if($isReg != NULL){
			$this->redirect('index', '',3,'This user has registered!');
		}else{
			$User->add($data);
			$this->redirect('index', '',3,'A new user is created!');
		}
	}
	/**
	 * @todo Update a user's infomation
	 */
	public function update(){
		$User = M('User');
		$data['uid'] = $_POST['uid'];
		$data['password'] = md5($_POST['oldpassword']);
		$data['state']=1;
		$getuser=$User->where('uid='.$data['uid'])->find();
		if($data['password'] == $getuser['password']){
			$info['uid'] = $data['uid'];
			$info['password'] = md5($_POST['newpassword']);
			$User->data($info)->save(); 
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','../Space/index');
			$this->success('用户信息更新成功');
		}else{
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','../Space/index');
			$this->error('输入的原密码不对，用户信息更新失败');
		}
	}
/**
	 * @todo Update a user's infomation
	 */
	public function editpage(){
		$uid = $_GET['uid'];
		$User = M('User');
		$thisuser = $User->where('uid='.$uid)->find();
		$this->assign('thisuser',$thisuser);
		$this->display();
	}
}
?>